package com.company.example.converter;

import com.company.example.core.util.DateUtil;
import com.company.example.core.util.StringUtil;
import org.springframework.core.convert.converter.Converter;

import java.util.Date;

/**
 * Created by ywl on 2017/1/9.
 */
public class String2DateConverter implements Converter<String,Date>{
    private String format = "yyyy-MM-dd HH:mm:ss";

    public void setFormat(String format) {
        if(!StringUtil.isEmpty(format)) {
            this.format = format;
        }
    }

    @Override
    public Date convert(String source) {
        try {
            return DateUtil.parse(source,format);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
